package com.jswlw.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Author: zhangpei
 * @Date: 2018/6/26 19:49
 * @Description: 连接池配置
 */
@Configuration
@MapperScan(basePackages = {"com.jswlw.dao"})
public class DruidConfig {

    @Autowired
    DruidPropertyConfig config;


    @Bean
    public DataSource dataSource() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(config.url);
        dataSource.setUsername(config.username);
        dataSource.setPassword(config.password);
        dataSource.setDriverClassName(config.driverClassName);
        dataSource.setInitialSize(config.initialSize);
        dataSource.setMaxActive(config.maxActive);
        dataSource.setMinIdle(config.minIdle);
        dataSource.setMaxWait(config.maxWait);
        dataSource.setTimeBetweenEvictionRunsMillis(config.timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(config.minEvictableIdleTimeMillis);
        dataSource.setValidationQuery(config.validationQuery);
        dataSource.setTestWhileIdle(config.testWhileIdle);
        dataSource.setTestOnBorrow(config.testOnBorrow);
        dataSource.setTestOnReturn(config.testOnReturn);
        dataSource.setPoolPreparedStatements(config.poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(config.maxPoolPreparedStatementPerConnectionSize);
        dataSource.setFilters(config.filters);
        List<Filter> filters = dataSource.getProxyFilters();
        boolean isExit = false;
        for (Filter filter : filters) {
            if (filter instanceof WallFilter) {
                ((WallFilter) filter).setConfig(wallConfig());
                isExit = true;
            }
        }
        if (!isExit) {
            filters = new ArrayList<>();
            filters.add(wallFilter());
            filters.add(wallFilter());
            dataSource.setProxyFilters(filters);
        }

        Properties prop = new Properties();
        prop.load(new ByteArrayInputStream(config.connectionProperties.replace(";", "\n").getBytes()));
        dataSource.setConnectProperties(prop);
        dataSource.setUseGlobalDataSourceStat(config.useGlobalDataSourceStat);
        return dataSource;
    }

    @Bean
    public WallConfig wallConfig() {

        WallConfig config = new WallConfig();
        config.setMultiStatementAllow(true);//允许一次执行多条语句
        config.setNoneBaseStatementAllow(true);//允许非基本语句的其他语句
        return config;
    }

    @Bean
    WallFilter wallFilter() {

        WallFilter wfilter = new WallFilter();
        wfilter.setConfig(wallConfig());
        return wfilter;
    }

}
